package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.annotation.InterfaceParam;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.request.RestTemplateUtils;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.project.ProjectUtils;

import java.util.*;

@SuppressWarnings("all")
@MarkAsApi
public class GetWeatherInfo extends InterfaceHandler {
    @InterfaceParam(value = "longitude", require = true, comment = "经度")
    @InterfaceParam(value = "latitude", require = true, comment = "纬度")
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        Object longitude = paramsMap.get("longitude");
        Object latitude = paramsMap.get("latitude");
        final boolean checkLatitudeAndLongitude = Transformation.checkLatitudeAndLongitude(latitude, longitude);
        if (!checkLatitudeAndLongitude) {
            throw new InterfaceResponseException(ResponseCode.Error.getStatusCode(), "经纬度坐标参数错误:" + latitude + "," + longitude + ", (latitude >= -90 && latitude <= 90) && (longitude >= -180 && longitude <= 180)");
        }
        String url = "https://weatherapi.market.xiaomi.com/wtr-v3/weather/all?longitude=" + longitude + "&latitude=" + latitude + "&sign=zUFJoAR2ZVrDy1vF3D07&isGlobal=false&locale=zh_cn&appKey=sDe3f4S98sdmXspDU8SA";
        final Map weatherData = RestTemplateUtils.defaultInstance().get(url, Map.class);
        if (weatherData != null) {
            final DictMap weatherDict = ProjectUtils.getSettingByRedis("weather.dict");
            weatherData.remove("cacheDate");
            weatherData.put("dict", weatherDict);
            weatherData.put("css", "");
        }
        return weatherData;
    }
}